package com.cxh.rmsq.utils.dialog

import android.Manifest
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.text.TextUtils
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ToastUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.ui.dialog.PhotoDialog
import com.cxh.rmsq.utils.loadImage
import com.cxh.rmsq.utils.permission.AppPermissionUtil
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeImageView
import com.hjq.shape.view.ShapeTextView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import kotlin.math.roundToInt


class CommentPopup(
    context: Context,
    flag: Int,
    click: onClick
) :
    BottomPopupView(context) {

    interface onClick {
        fun click(content: String, s1: String)
        fun uploadImg(s3: String, s4: String)
    }

    //    private val viewModels by lazy {
//        ViewModelProvider.NewInstanceFactory().create(UserInfoViewModel::class.java)
//    }
    var func: onClick
    var flag: Int
    private val siv_img: ShapeImageView by lazy { findViewById<ShapeImageView>(R.id.siv_img) }
    private val iv_del: ImageView by lazy { findViewById<ImageView>(R.id.iv_del) }

    private var photoDialog: PhotoDialog? = null
    private var commentImg = ""

    init {
        this.func = click
        this.flag = flag
    }

    override fun getImplLayoutId(): Int {
        return R.layout.comment_popup
    }

    override fun onCreate() {
        super.onCreate()

        var tv_content = findViewById<ShapeEditText>(R.id.set_content)
        var stv_publish = findViewById<ShapeTextView>(R.id.stv_publish)
//         siv_img = findViewById<ShapeImageView>(R.id.siv_img)
//        var iv_del = findViewById<ImageView>(R.id.iv_del)

        initAvatar()

        iv_del.setOnClickListener {
            iv_del.isVisible = false
            siv_img.setImageDrawable(resources.getDrawable(R.mipmap.icon_add_img_publish_baseinfo))
        }
        siv_img.setOnClickListener {
//            func.uploadImg( "", "")
            if (ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                // 显示对话框告知用户权限使用目的
                val builder: AlertDialog.Builder = AlertDialog.Builder(context)
                builder.setTitle("权限申请")
                    .setMessage("我们需要访问您的相机、存储权限，以便于图片上传、好友间发送照片以及实名认证等操作")
                    .setPositiveButton("允许",
                        DialogInterface.OnClickListener { dialog, which ->
                            AppPermissionUtil.requestPermissions(context,
                                arrayOf<String>(
                                    Manifest.permission.CAMERA,
                                    Manifest.permission.READ_EXTERNAL_STORAGE
                                ),
                                object : AppPermissionUtil.OnPermissionListener {
                                    override fun onPermissionGranted() {
                                        photoDialog?.show()
                                    }

                                    override fun onPermissionDenied() {

                                    }

                                })
                        })
                    .setNegativeButton(
                        "拒绝",
                        DialogInterface.OnClickListener { dialog, which ->
                            // 用户拒绝权限申请
                        })
                    .show()
            } else {
                // 已获得权限，执行相关操作
                photoDialog?.show()
            }

        }
        stv_publish.setOnClickListener {
            if (TextUtils.isEmpty(tv_content.text.toString().trim())) {
                ToastUtils.showShort("请输入评论内容")
                return@setOnClickListener
            }
            func.click(
                tv_content.text.toString().trim(),
                "",
            )
//            dismiss()
        }
//        viewModels.upload().observe(this, object : Observer<Resource2<String?>?> {
//            override fun onChanged(resource: Resource2<String?>?) {
//                if (resource!!.status == Status.SUCCESS && resource.data != null) {
//                    commentImg = resource.data
//                }
//            }
//        })
    }

    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return ((XPopupUtils.getAppHeight(context) * .95f).roundToInt())
    }


    private fun initAvatar() {
        if (photoDialog == null) {
            photoDialog = PhotoDialog(context, true, 1, 1, R.style.BottomDialog)
        }
        photoDialog!!.setOnClickListener(object : PhotoDialog.OnClickListener {
            override fun onPhotoPathResult(images: MutableList<String>) {
//                viewModels.uploadImg(images[0])
                loadImage(images[0], siv_img)
                func.uploadImg(images[0], "")
                iv_del.isVisible = true
            }

            override fun onCameraPathResult(images: MutableList<String>) {
//                viewModels.uploadImg(images[0])
                loadImage(images[0], siv_img)
                func.uploadImg(images[0], "")
                iv_del.isVisible = true
            }
        })
    }

}